from django import template
from django.conf import settings
from django.utils import safestring

register = template.Library()

@register.simple_tag
def fb_base_js():
  return safestring.mark_safe(
    """
    <div id="fb-root"></div>
    <script src="http://connect.facebook.net/en_US/all.js"></script>
    <script>
      FB.init({
        appId:'%s',
        cookie:true,
        status:true,
        xfbml:true
      });
      FB.Event.subscribe(
        'auth.sessionChange',
        function(response) {
          window.location.reload();
        });
    </script> 
    """ % settings.FACEBOOK_APP_ID
  )

@register.simple_tag
def fb_meta_tags():
  tags = [
    '<meta property="fb:app_id" content="%s" />' % settings.FACEBOOK_APP_ID,
    '<meta property="og:site_name" content="%s" />' % settings.FACEBOOK_APP_NAME,
    '<meta property="fb:admins" content="%s" />' % ','.join(settings.FACEBOOK_ADMINS),
  ]
  return safestring.mark_safe('\n'.join(tags))
